package com.oocl.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.oocl.dao.FoodDao;
import com.oocl.dao.impl.FoodDaoImpl;
import com.oocl.dto.FoodDTO;
import com.oocl.exception.MyException;
import com.oocl.pojo.Food;
import com.oocl.service.FoodService;
import com.oocl.util.Base64Util;
import com.oocl.util.PropertiesFactory;

/*
 * @author Garen chen
 * @description : food service
 * @date 2017-07-28
 * */
public class FoodServiceImpl implements FoodService{
      private FoodDao fDao;
      
      
      public FoodServiceImpl(){
    	  this.fDao = new FoodDaoImpl();
    	  
      }
      
      /*
  	 * @method : loadAll
  	 * @function : load all food list 
  	 * @param 
  	 * @return  Food List
  	 * @date 2017-07-28
  	 * @author Garen chen
  	 * */
      public List<Food> loadAll() throws MyException {
    	  List<Food> list = null;
  		try {
  			list = fDao.loadAll();
  		} catch (SQLException e) {
  			throw new MyException("查询出错",e.getClass().getName());
  		}
  		return list;
      }
      
	/*
	 * @method : loadFoodsByMid
	 * @function : load food list by mid
	 * @param  mid :Muser id
	 * @return  Food List
	 * @date 2017-07-28
	 * @author Garen chen
	 * */
	public List<FoodDTO> loadFoodDTOs(String icnum,String type) throws MyException {
		List<FoodDTO> list = new ArrayList<FoodDTO>();
		List<Food> list1=null;
		try {
			if(null==type){
				list1 = fDao.loadFoodsByIcnum(icnum);				
			}else{
				list1 = fDao.loadFoodsByType(icnum, type);
			}
			for(Food f:list1){
				InputStream in = new FileInputStream(PropertiesFactory.getPro().getProperty("fileroot")+File.separator+f.getFimage());
				byte[] bs = new byte[in.available()];
				in.read(bs);
				in.close();
				FoodDTO fd = new FoodDTO(f.getFid(), f.getIcnum(),f.getFname(), f.getFtype(),
						Base64Util.wrapBase64forjsp(bs), f.getFprice());
				f.setFimage(Base64Util.wrapBase64forjsp(bs));
				list.add(fd);
			}
		}catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (SQLException e) {
			throw new MyException("查询出错",e.getClass().getName());
		}
		return list;
	}
   
	/*
	 * @method : loadFoodsByType
	 * @function : load food list by mid and  food type
	 * @param  mid :Muser id ; type :food type
	 * @return  Food List
	 * @date 2017-07-28
	 * @author Garen chen
	 * */
	public List<Food> loadFoodsByType(String icnum, String type)
			throws MyException {
		List<Food> list = null;
		try {
			list = fDao.loadFoodsByType(icnum,type);
		} catch (SQLException e) {
			throw new MyException("查询出错",e.getClass().getName());
		}
		return list;
	}

	/*
	 * @method : addFood
	 * @function : add one Food 
	 * @param  f Food
	 * @return  Food id
	 * @date 2017-07-28
	 *  @author Garen chen
	 * */
	public String  addFood(Food f) throws MyException {
		String id = null;
		try {
			id = fDao.addFood(f);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new MyException("添加出错",e.getClass().getName());
		}
		return id;
	}
  
	/*
	 * @method : update
	 * @function : update one Food 
	 * @param  f Food
	 * @return  update number
	 * @date 2017-07-28
	 *  @author Garen chen
	 * */
	public int update(Food f) throws MyException {
		int count = -1;
		try {
			count = fDao.update(f);
		} catch (SQLException e) {
			throw new MyException("更新出错",e.getClass().getName());
		}
		return count;
	}

	
	/*
	 * @method : delete
	 * @function : delete one Food 
	 * @param  fid : Food id
	 * @return  delete number
	 * @date 2017-07-28
	 *  @author Garen chen
	 * */
	public int delete(String fid) throws MyException {
		int count = -1;
		try {
			count = fDao.delete(fid);
		} catch (SQLException e) {
			throw new MyException("删除出错",e.getClass().getName());
		}
		return count;
	}
     
	/*
	 * @method : loadOne
	 * @function : load one Food 
	 * @param  fid : Food id
	 * @return  Food 
	 * @date 2017-07-28
	 *  @author Garen chen
	 * */
	public Food loadOne(String fid) throws MyException {
		Food food = null;
		try {
			food = fDao.loadOne(fid);
		} catch (SQLException e) {
			throw new MyException("查询出错",e.getClass().getName());
		}
		return food;
	}
    
	/*
	 * @method : loadFoodsByMid
	 * @function : load food list by icnum
	 * @param  mid :Muser icnum
	 * @return  Food List
	 * @date 2017-07-28
	 * @author Garen chen
	 * */
	public List<Food> loadFoodsByIcnum(String icnum )
			throws MyException {
		List<Food>  list = new ArrayList<Food>();
		try {
			System.out.println(icnum);
			List<Food> list1 = fDao.loadFoodsByIcnum(icnum);
			for(Food f:list1){
				InputStream in = new FileInputStream(PropertiesFactory.getPro().getProperty("fileroot")+PropertiesFactory.getPro().getProperty("foodpath")+File.separator+f.getFimage());
				byte[] bs = new byte[in.available()];
				in.read(bs);
				in.close();
				f.setFimage(Base64Util.wrapBase64forjsp(bs));
				list.add(f);
			}
		}catch (SQLException e) {
			e.printStackTrace();
			throw new MyException("查询出错",e.getClass().getName());
		}catch (IOException e) {
			throw new MyException("文件读取出错",e.getClass().getName());
		}
		return list;
	}

	

	

	


}
